﻿@namespace MXERP.Blazor.Components
@typeparam T
@inherits MudComponentBase

<CascadingValue IsFixed="true" Value="this">
    @if (Columns != null)
    {
        @Columns
    }
</CascadingValue>
<CascadingValue IsFixed="true" Value="this">
    <div @attributes="UserAttributes" class="@ClassName" style="@_style">
        <div class="@TableClassName" style="@TableStyle" @ref="_tableElement">
            <table class="md-table-root md-table-root-fixed">
                <thead class="@HeadClassName">
                    <tr class="md-table-row">
                        @foreach (var column in RenderedColumns)
                        {
                            <MXERP.Blazor.Components.HeaderCell T="T" Column="@column"></MXERP.Blazor.Components.HeaderCell>
                        }
                    </tr>
                </thead>
                <tbody class="@BodyClassName">
                    @if (Items != null && Items.Count() > 0)
                    {
                        @Body(Items, default, null)
                    }
                </tbody>
            </table>
        
        </div>
    </div>
</CascadingValue>

@code{
    internal RenderFragment Body(IEnumerable<T> items, T? parent, int? level)
    {
        var l = level == null || level == 0 ? 0 : level.Value;
        return 
            @<MudVirtualize @ref="_mudVirtualize" ItemsProvider="@VirtualItemsProvider" Enabled="@Virtualize" Items="items.ToList()" OverscanCount="@OverscanCount" ItemSize="@ItemSize" Context="item">
                <Placeholder>
                    <tr class="md-table-row">
                        <td class="md-table-cell" colspan="1000">
                        </td>
                    </tr>
                </Placeholder>
                <ChildContent>
                    <TableRow @key="item" Level="@l" Item="item" Parent="parent"></TableRow>
                </ChildContent>
            </MudVirtualize>;
    }
}